package top.jandmla.excel.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.jandmla.excel.bean.Role;
import top.jandmla.excel.dao.RoleMapper;
import top.jandmla.excel.pojo.RolePojo;
import top.jandmla.excel.service.RoleService;
import top.jandmla.excel.util.result.BaseResult;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public BaseResult<String> deleteRoleById(Role role) {
        if (role == null || StringUtils.isEmpty(role.getId()))
            return BaseResult.fail(1,"传输参数异常！");
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq("id",role.getId());
        return roleMapper.delete(qw) > 0 ? BaseResult.success("删除成功！") : BaseResult.fail(1,"删除失败，发生未知错误！");
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public BaseResult<String> insertRole(Role role) {
        if (role == null || StringUtils.isEmpty(role.getRoleName()))
            return BaseResult.fail(1,"参数传输异常！");
        role.setId(UUID.randomUUID().toString().replaceAll("-",""));
        return roleMapper.insert(role) > 0 ? BaseResult.success("添加成功！") : BaseResult.fail(1,"添加失败，出现未知错误！");
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=true,rollbackFor={Exception.class})
    public List<RolePojo> getAllRolePojo(Role role) {
        return roleMapper.getAllRolePojo(role);
    }
}
